package com.jxs.android.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.util.Log;

public class OpTimer {

	private int index = 4;
	private Date start;
	private Date end;

	public OpTimer(int index) {
		super();
		this.index = index;
		if (index == 1) {
			initThisWeek();
		} else if (index == 2) {
			initThisMonth();
		} else if (index == 3) {
			initLast3Month();
		}
	}

	public int getIndex() {
		return index;
	}

	private void initThisWeek() {
		Calendar calendar = Calendar.getInstance();

		int paWeek = calendar.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : calendar.get(Calendar.DAY_OF_WEEK) - 1;

		switch (paWeek) {// 当前星期
		case 1:// 星期一
			start = calendar.getTime();
			calendar.add(Calendar.DATE, 6);
			end = calendar.getTime();
			break;
		case 2:// 星期二
			calendar.add(Calendar.DATE, -1);
			start = calendar.getTime();
			calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, 5);
			end = calendar.getTime();
			break;
		case 3:// 星期三
			calendar.add(Calendar.DATE, -2);
			start = calendar.getTime();
			calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, 4);
			end = calendar.getTime();
			break;
		case 4:// 星期四
			calendar.add(Calendar.DATE, -3);
			start = calendar.getTime();
			calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, 3);
			end = calendar.getTime();
			break;
		case 5:// 星期五
			calendar.add(Calendar.DATE, -4);
			start = calendar.getTime();
			calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, 2);
			end = calendar.getTime();
			break;
		case 6:// 星期六
			calendar.add(Calendar.DATE, -5);
			start = calendar.getTime();
			calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, 1);
			end = calendar.getTime();
			break;
		case 7:// 星期日
			end = calendar.getTime();
			calendar.add(Calendar.DATE, -6);
			start = calendar.getTime();
			break;
		}

		Log.e("TAG", paWeek + "--" + getStartTime() + "--" + getEndTime());

	}

	private void initThisMonth() {
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.DATE, 1);
		start = calendar.getTime();
		// 本月的最后一天
		calendar.set(Calendar.DATE, 1);
		calendar.roll(Calendar.DATE, -1);
		end = calendar.getTime();
	}

	private void initLast3Month() {
		Calendar calendar = Calendar.getInstance();
		this.end = calendar.getTime();
		calendar.add(Calendar.MONTH, -2); // 开始+6，获取周结束日期
		this.start = calendar.getTime();
	}

	public String getStartTime() {
		if (index != 4) {
			return Timer.getSDFyyyyMMdd().format(start);
		}
		return "";
	}

	public String getEndTime() {
		if (index != 4) {
			return Timer.getSDFyyyyMMdd().format(end);
		}
		return "";

	}

	public static void main(String[] args) {
		OpTimer s = new OpTimer(3);
		System.out.println(s.getStartTime());
		System.out.println(s.getEndTime());
	}

}
